package com.zhang.controller.system;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.common.exception.myexception.CaptchaException;
import com.zhang.common.util.redis.RedisUtil;
import com.zhang.common.util.result.R;
import com.zhang.common.util.result.RUtils;
import com.zhang.system.dto.LoginDto;
import com.zhang.system.entity.AdminUser;
import com.zhang.system.entity.UserInfoVo;
import com.zhang.system.entity.UserSearch;
import com.zhang.system.service.IAdminUserService;
import com.zhang.system.vo.AdminUserCompositionStatisticVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 系统用户表 前端控制器
 * </p>
 *
 * @author 战神
 * @since 2023-07-02
 */
@Controller
@Slf4j
@ResponseBody
@RequestMapping("/system/adminUser/")
@SaCheckLogin
public class AdminUserController {
    @Resource
    IAdminUserService adminUserService;
    @Autowired
    private RedisUtil redisUtil;


    @PostMapping("login")
    @SaIgnore
    R adminLogin(@RequestBody LoginDto loginDto) {
        String captchaKey = loginDto.getCaptchaKey();
        if (captchaKey != null) {
            String Captchavalue = redisUtil.getToString(captchaKey);
            log.info("验证码key为captchaKey:{}：正确的验证码为:code:{}", captchaKey, Captchavalue);
            if (Captchavalue == null) {
                throw new CaptchaException("验证码不存在！");
            } else {
                String captchaValue = loginDto.getCaptchaValue();
                if (!captchaValue.equals(Captchavalue)) {
                    redisUtil.del(captchaKey);
                    throw new CaptchaException("验证码输入错误！");
                }
                redisUtil.del(captchaKey);
            }
        } else throw new CaptchaException("请输入验证码！");
        R r = adminUserService.adminUserLogin(loginDto.getLoginKey(), loginDto.getPassword(), loginDto.getType());
        return r;
    }

    @GetMapping("getMyInfo")
    R getMyInfo() {
        Long userId = StpUtil.getLoginIdAsLong();
        UserInfoVo userInfVo = adminUserService.getUserInfVo(userId);
        return RUtils.successData(userInfVo);
    }

    @GetMapping("getUserInfo")
    @SaIgnore
    R getUserInfo(@RequestParam Long userId) {
        AdminUser adminUser = adminUserService.getUserInfo(userId);
        return RUtils.successData(adminUser);
    }

    @PostMapping("addAdminUser")
    @SaCheckPermission("user_add")
    R createAdminUser(@Valid @RequestBody AdminUser adminUser) {
        return adminUserService.addAdminUser(adminUser);
    }

    @PostMapping("delete")
    @SaCheckPermission("user_delete")
    R deleteAdminUser(@RequestBody AdminUser adminUser) {
        if (!adminUserService.isAdmin(adminUser.getId())) {
            adminUserService.removeById(adminUser);
            return RUtils.successMsg("删除成功");
        }
        return RUtils.err("超级管理员不能删除");
    }

    @PutMapping("updateAdminUser")
    @SaCheckPermission("user_update")
    R updateAdminUser(@RequestBody AdminUser adminUser) {
        return adminUserService.updateAdminUser(adminUser);
    }

    @RequestMapping("logout")
    @SaIgnore
    public R logout() {
        StpUtil.logout();
        return RUtils.successMsg("注销成功");
    }

    @GetMapping("getAllUsers/{pageNum}/{pageSize}")
    public R getAllUsers(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        /*分页获取*/
        Page<AdminUser> page = adminUserService.page(new Page<>(pageNum, pageSize));
        return RUtils.successData(page);
    }

    @PostMapping("get/user/list/from/roleKeys")
    public R getUserListFromRole(@RequestBody List<String> rolekeys) {
        List<AdminUser> list = adminUserService.getUserListFromRoleKeys(rolekeys);
        return RUtils.successData(list);
    }

    @PostMapping("UserSearch")
    public R UserSearch(@RequestBody UserSearch userSearch) {
        List<AdminUser> userList = adminUserService.userSeach(userSearch);
        return RUtils.successData(userList);
    }

    /**用户组成图数据
     * @return {@link R}
     */
    @GetMapping("userCompositionStatistic")
    public R UserCompositionStatistic() {
        AdminUserCompositionStatisticVO vo = adminUserService.UserCompositionStatistic();
        return RUtils.successData(vo);
    }

}
